package com.qc31.monitorlib.uitls

import com.qc31.baselib.entity.ToastEntity
import com.qc31.baselib.net.AppContext
import com.qc31.baselib.utils.MLog
import com.qc31.comlib.net.ServiceHelper
import com.qc31.monitorlib.R
import io.reactivex.rxjava3.subjects.PublishSubject

/** APP设置工具类 */
object AppConfigUtil {
    private val _saveLive = PublishSubject.create<Boolean>()
    val saveLive = _saveLive.hide()

    private val _toastLive = PublishSubject.create<ToastEntity>()
    val toastLive = _toastLive.hide()

    //infoSwitch：通知开关；homeSet：首页设置；quickSet：快捷设置；appGpsColumn：定位信息；mapMarker：地图图标
    private val keys = arrayOf("infoSwitch", "homeSet", "quickSet", "appGpsColumn", "mapMarker")

    /** 保存设置参数 */
    fun saveConfig(keyIndex: Int, data: String) {
        MLog.e("${keys[keyIndex]}  data=    $data")
        ServiceHelper.getRepository().saveAppConfig(keys[keyIndex], data).subscribe({
            _saveLive.onNext(true)
            _toastLive.onNext(ToastEntity(toast = it.result.toString(), isShowLoading = false))
        }, {
            _saveLive.onNext(false)
            if (it.message == AppContext.getDescById(R.string.net_error_404)) {
                return@subscribe
            }
            _toastLive.onNext(ToastEntity(it.message ?: ""))
        })
    }

    /** 保存单车设置参数 */
    fun saveCarConfig(carId: String, data: String) {
        MLog.e("上传设置====   $carId   $data")
        ServiceHelper.getRepository().saveCarCfg("[$carId]", data).subscribe({
            _saveLive.onNext(true)
            _toastLive.onNext(ToastEntity(toast = it.result.toString(), isShowLoading = false))
        }, {
            _saveLive.onNext(false)
            if (it.message == AppContext.getDescById(R.string.net_error_404)) {
                return@subscribe
            }
            _toastLive.onNext(ToastEntity(it.message ?: ""))
        })
    }
}